package com.yu.work.service;

import com.yu.work.dao.RecordDao;
import com.yu.work.domain.Record;

import java.util.Date;
import java.util.List;

/**
 * 采用单例模式，这样节约资源
 */
public class RecordService {

    private static RecordService mRecordService;
    private RecordDao dao = new RecordDao();

    public static RecordService getInstance() {
        if (mRecordService == null) {
            mRecordService = new RecordService();
        }

        return mRecordService;
    }

    private RecordService() {
    }

    public void save(Record record) {
        record.setCreateTime(new Date());
        dao.save(record);
    }

    public Record get(Long id) {
        return dao.get(id);
    }

    /**
     * 检查唯一性
     * 唯一返回true，不唯一返回false
     * @param targetNumber
     * @return
     */
    public boolean isUnique(Long targetNumber) {
        if (dao.getByTargetNumber(targetNumber) == null) {
            return true;
        }

        return false;
    }

    public List<Record> getAll() {
        return dao.getAll();
    }

    public void delete(Long id) {
        dao.delete(id);
    }

    public void update(Record record) {
        record.setUpdateTime(new Date());
        dao.update(record);
    }
}
